<template>
  <div class="state-transition-demo">
    <h2>状态过渡动画</h2>

    <div class="number-demo">
      <h3>数值动画</h3>
      <div class="number-display">
        {{ animatedNumber.toFixed(0) }}
      </div>
      <div class="controls">
        <button @click="updateNumber(0)">重置</button>
        <button @click="updateNumber(1000)">1000</button>
        <button @click="updateNumber(5000)">5000</button>
        <button @click="updateNumber(10000)">10000</button>
      </div>
    </div>

    <div class="progress-demo">
      <h3>进度条动画</h3>
      <div class="progress-container">
        <div class="progress-bar" :style="{ width: animatedProgress + '%' }">
          {{ animatedProgress.toFixed(1) }}%
        </div>
      </div>
      <div class="controls">
        <button @click="updateProgress(0)">0%</button>
        <button @click="updateProgress(25)">25%</button>
        <button @click="updateProgress(50)">50%</button>
        <button @click="updateProgress(75)">75%</button>
        <button @click="updateProgress(100)">100%</button>
      </div>
    </div>

    <div class="color-demo">
      <h3>颜色过渡</h3>
      <div class="color-box" :style="{ backgroundColor: currentColor }">
        当前颜色：{{ currentColor }}
      </div>
      <div class="controls">
        <button @click="updateColor('#e74c3c')">红色</button>
        <button @click="updateColor('#2ecc71')">绿色</button>
        <button @click="updateColor('#3498db')">蓝色</button>
        <button @click="updateColor('#f39c12')">橙色</button>
        <button @click="updateColor('#9b59b6')">紫色</button>
      </div>
    </div>
  </div>
</template>
<script setup>
import { ref, watch } from 'vue'

// 数值动画
const number = ref(0)
const animatedNumber = ref(0)

const updateNumber = (newValue) => {
  number.value = newValue
}

watch(number, (newValue) => {
  const startValue = animatedNumber.value
  const duration = 1000 // 动画持续时间
  const startTime = Date.now()

  const animate = () => {
    const now = Date.now()
    const progress = Math.min((now - startTime) / duration, 1)

    // 使用缓动函数
    const easeOutQuart = 1 - Math.pow(1 - progress, 4)

    animatedNumber.value = startValue + (newValue - startValue) * easeOutQuart

    if (progress < 1) {
      requestAnimationFrame(animate)
    }
  }

  animate()
})

// 进度条动画
const progress = ref(0)
const animatedProgress = ref(0)

const updateProgress = (newValue) => {
  progress.value = newValue
}

watch(progress, (newValue) => {
  const startValue = animatedProgress.value
  const duration = 800
  const startTime = Date.now()

  const animate = () => {
    const now = Date.now()
    const progress = Math.min((now - startTime) / duration, 1)

    const easeOutCubic = 1 - Math.pow(1 - progress, 3)

    animatedProgress.value = startValue + (newValue - startValue) * easeOutCubic

    if (progress < 1) {
      requestAnimationFrame(animate)
    }
  }

  animate()
})

// 颜色过渡
const currentColor = ref('#3498db')

const updateColor = (newColor) => {
  currentColor.value = newColor
}
</script>
<style scoped>
.state-transition-demo {
  padding: 20px;
  max-width: 800px;
  margin: 0 auto;
}

.number-demo,
.progress-demo,
.color-demo {
  margin-bottom: 40px;
  padding: 20px;
  border: 1px solid #e1e5e9;
  border-radius: 8px;
  background: white;
}

.number-display {
  font-size: 3em;
  font-weight: bold;
  text-align: center;
  color: #2c3e50;
  margin: 20px 0;
  font-family: 'Courier New', monospace;
}

.progress-container {
  background: #ecf0f1;
  border-radius: 10px;
  overflow: hidden;
  margin: 20px 0;
  height: 40px;
}

.progress-bar {
  height: 100%;
  background: linear-gradient(90deg, #3498db, #2ecc71);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: bold;
  transition: width 0.1s ease;
  min-width: 60px;
}

.color-box {
  height: 100px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: bold;
  margin: 20px 0;
  transition: background-color 0.5s ease;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
}

.controls {
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-wrap: wrap;
}

.controls button {
  padding: 8px 16px;
  background: #3498db;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.controls button:hover {
  background: #2980b9;
}

h3 {
  color: #2c3e50;
  margin-bottom: 15px;
}
</style>
